/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.bourre.log.PixlibStringifier;
import com.kairos.motion.strategy.IPlayStrategy;

/**
 * The {@code AdvancedLoopStrategy} bring together the {@code LoopStrategy}
 * and the {@code NoLoopStrategy}.
 * 
 * <p>The {@code isMotionFinished} is the same as the {@code NoLoopStrategy}
 * one and the {@code getNextFrame} is the {@code LoopStrategy} one.</p>
 * 
 * @author 	Cédric Néhémie
 * @see		IPlayStrategy
 */
class com.kairos.motion.strategy.AdvancedLoopStrategy implements IPlayStrategy 
{
	/**
	 * Returns {@code false} when the animation playhead 
	 * go on or further the last frame during the current move.
	 * 
	 * @param	frame		The current frame in the {@code MovieClip}
	 * @param	totalFrames	The number of frames in the {@code MovieClip}
	 * @param	step		How many frames the playhead have to move.
	 * @return	{@code true} if the animation have to stop after the
	 * 			playhead moves, either {@code false}
	 */
	public function isMotionFinished(frame : Number, totalFrames : Number, step : Number) : Boolean 
	{
		return frame + step > totalFrames;
	}
	
	/**
	 * Returns the next frame wherein the playhead can move.
	 * 
	 * <p>When the playhead is going to go past the last frame
	 * the animation loop and the playhead follow its move from
	 * first frame.</p>
	 * 
	 * @param	frame		The current frame in the {@code MovieClip}
	 * @param	totalFrames	The number of frames in the {@code MovieClip}
	 * @param	step		How many frames the playhead have to move.
	 * @return	A valid frame index in the {@code MovieClip}
	 */
	public function getNextFrame(frame : Number, totalFrames : Number, step : Number) : Number 
	{
		return isMotionFinished ( frame, totalFrames, step ) ?
					( 1 + frame + step ) % totalFrames :
					frame + step;
	}

	/**
	 * Return the String representation of the object. 
	 */	
	public function toString () : String
	{
		return PixlibStringifier.stringify( this );
	}
}